package com.moyu.user.controller.permission;

import com.moyu.common.core.controller.BaseController;
import com.moyu.common.core.domain.R;
import com.moyu.common.core.domain.TableDataInfo;
import com.moyu.user.domain.permission.dto.PermissionQueryDTO;
import com.moyu.user.domain.permission.dto.PermissionSaveDTO;
import com.moyu.user.domain.permission.vo.PermissionVO;
import com.moyu.user.service.permission.IPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 权限控制层
 *
 * @Author 赵彦博
 * @Date 2024/12/27 15:33
 */
@RestController
@RequestMapping("/permission")
@Tag(name = "权限接口")
public class PermissionController extends BaseController {

    @Autowired
    private IPermissionService permissionService;

    @PostMapping("/add")
    @Operation(summary = "新增权限", description = "根据提供的信息新增权限")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "5002", description = "权限已存在")
    public R<Void> add(@RequestBody PermissionSaveDTO permissionSaveDTO) {
        return toR(permissionService.add(permissionSaveDTO));
    }

    @PostMapping("/addPermissionWithRole/{id}/permissions")
    @Operation(summary = "为角色新增权限", description = "根据提供的信息新增权限")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "5002", description = "权限已存在")
    public R<Void> addPermissionWithRole(@PathVariable Long id, @RequestBody List<PermissionVO> permissions) {
        return toR(permissionService.addPermissionWithRole(id, permissions));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除权限", description = "通过用户id删除权限")
    @Parameters(value = {
            @Parameter(name = "roleId", in = ParameterIn.PATH, description = "权限ID")
    })
    @ApiResponse(responseCode = "1000", description = "成功删除权限")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "5001", description = "权限不存在")
    public R<Void> delete(@PathVariable Long id) {
        return toR(permissionService.delete(id));
    }

    @GetMapping("/list")
    @Operation(summary = "查询权限列表", description = "根据提供的信息查找权限列表")
    @ApiResponse(responseCode = "1000", description = "成功查询权限列表")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    public TableDataInfo list(PermissionQueryDTO permissionQueryDTO) {
        return getTableDataInfo(permissionService.list(permissionQueryDTO));
    }

    @GetMapping("/listByRoleId")
    @Operation(summary = "根据角色ID查询权限列表", description = "根据提供的信息查找权限列表")
    @ApiResponse(responseCode = "1000", description = "成功查询权限列表")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    public TableDataInfo listByRoleId(PermissionQueryDTO permissionQueryDTO) {
        return getTableDataInfo(permissionService.listByRoleId(permissionQueryDTO));
    }

    @GetMapping("/listByRoleIdAll")
    @Operation(summary = "根据角色ID查询所有权限", description = "根据提供的信息查找权限列表")
    @ApiResponse(responseCode = "1000", description = "成功查询权限列表")
    @ApiResponse(responseCode = "2000", description = "服务繁忙请稍后重试")
    public List<PermissionVO> listByRoleIdAll(Long id) {
        return permissionService.listByRoleIdAll(id);
    }

}
